layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    var tableIns = table.render({
        elem: '#list',
        url : '/roleAdmin/list/',
        text: { none: "Aucun résultat"},
        cellMinWidth : 95,
        height : "full-125",
        id : "listTable",
        cols : [[
            {field: 'roleName', title: "角色名称", align:"center"},
            {field: 'code', title: "角色类型", align:'center', templet : function( d){
                switch ( d.code) {
                    case "LEVEL_SUPER":return "超级管理员";
                    case "LEVEL_ADMIN":return "员工角色";
                    case "LEVEL_EMPLOY":return "管理员角色";
                }
            }},
            {field: 'createTime', title: '添加时间', align:'center', templet : function( d){
                return baseConfig.formatDate( d.createTime);
            }},
            {title: 'État', templet:'#listBar',fixed:"right", align:"center"}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "data": res.data //解析数据列表
            };
        }
    });
    //获取浏览器最大高度，以防页面显示不完全
    var maxHeight = $(window).height();

    //添加/修改/详细查看（readonly=true）
    function addOrUpdateOrDetail( data, readonly){
        var index = layui.layer.open({
            title : "添加",
            type : 2,
            area: [1000 + 'px', (maxHeight < 550 ? maxHeight-10 : 550) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "role", //设定一个id，防止重复弹出
            content : "formData.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                if( data){
                    body.find( "input[name=id]").val( data.id).attr( "readonly", readonly);//标识是修改还是新增
                    body.find( "input[name=roleName]").val( data.roleName).attr( "readonly", readonly);
                    body.find( "input[name=code]").val( data.code).attr( "readonly", readonly);
                    var roles = data.content.split( ",");
                    body.find( 'input[type=checkbox]').each( function() {
                        for(var i = 0; i < roles.length; i++){
                            if( roles[i] == $(this).val()){
                                $(this).attr( "checked", true);
                                break;
                            }
                        }

                    });
                    if( readonly){
                        body.find( "#submitDiv").hide();
                    }
                    form.render();
                }
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //删除
    function del( ids){
        layer.confirm('确定删除选中的数据项？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/roleAdmin/del",{
                "ids" : ids
            },function( res){
                if( res.code == 200){
                    tableIns.reload();
                    layer.close( index);
                    top.layer.msg( res.msg);
                }else{
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                }
            });
        });
    }

    //添加
    $(".add_btn").click(function(){
        addOrUpdateOrDetail();
    });

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            if( data.code == "LEVEL_SUPER"){
                top.layer.msg( "超级管理员不能编辑");
                return;
            }
            addOrUpdateOrDetail( data, false);
        } else if(layEvent === 'del'){ //删除
            if( data.code == "LEVEL_SUPER"){
                top.layer.msg( "超级管理员不能删除");
                return;
            }
            del( data.id);
        } else if(layEvent === 'look'){ //预览
            addOrUpdateOrDetail( data, true);
        }
    });
});
